package com.zegome.game.oanquan.scene;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Linear;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.zegome.facebook.IFacebookProcessorListener;
import com.zegome.game.MainActivity;
import com.zegome.game.main.ZegomeActivity;
import com.zegome.game.me.AutoRunText;
import com.zegome.game.oanquan.GameCenter;
import com.zegome.game.oanquan.HighScoreHelper;
import com.zegome.game.oanquan.ImageAssets;
import com.zegome.game.oanquan.OAnQuanController;
import com.zegome.game.oanquan.SoundAsset;
import com.zegome.game.oanquan.scene.play.PlayingScreen;
import com.zegome.game.util.helper.SettingManager;
import com.zegome.game.util.helper.StageHelper;
import com.zegome.game.util.helper.WidgetAccessor;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class UICenter implements IFacebookProcessorListener {

	// ===========================================================
	// Constants
	// ===========================================================
	public static final int UI_EMPTY = -1;
	public static final int DIALOG_EMPTY = -1;
	public static final int GLOBAL_DIALOG_EMPTY = -1;
	public static final int GLOBAL_HELP = 0;
	public static final int GLOBAL_INFO = 1;
	public static final int SHOW_ALERT_EXIT = 2;
	public static final int UI_MENU_1 = 3;
	public static final int UI_MENU_2 = 4;
	public static final int UI_MENU_3 = 5;
	public static final int UI_PLAYING = 6;
	public static final int SHOW_HIGH_SCORE = 7;
	public static final int SHOW_SAVE_SCORE = 8;
	public static final int SHOW_INDEX_SCORE = 9;
	public static final int SHOW_RESULT = 10;
	public static final int SHOW_PAUSE = 11;

	private static final String[] MESSAGE_REQUEST_HIGH_SCORE = {"đứng đầu", "xếp thứ hai", "xếp thứ ba", "xếp thứ tư", "xếp thứ năm", 
																"xếp thứ sáu", "xếp thứ bảy", "xếp thứ tám", "xếp thứ chín", "xếp thứ mười"};
	
	// ===========================================================
	// Fields
	// ===========================================================	
	private Sound mButtonClickSound;
	
	public Image mBlackImage;
	
	private Image mWinBoardImage;
	private Image mDrawBoardImage;
	
	private Label[] mWinPlayerLables;
	private Label[] mLossPlayerLables;

	/*
	 * highscore board
	 */
	private Label[][] mNameMarks;

	/*
	 * alert dialog board
	 */
	private Label mMessageAlert;

	private ImageButton mFbLogin;
	private ImageButton mFbRequest;
	private ImageButton mFbOpenAction;
	private ImageButton mFbNewFeed;
	
	private ImageButton mBackButton;
	
	private ImageButton mPlayButton;
	private ImageButton mHighScoreButton;
	
	private ImageButton mOnePlayerButton;
	private ImageButton mTwoPlayerButton;
	
	private ImageButton mEasyModeButton;
	private ImageButton mNormalModeButton;
	private ImageButton mHardModeButton;

	private Image mLogoImage;	
	private Image mHelpPopup;

	int mState = UI_EMPTY;
	int mLastState = UI_EMPTY;
	private int mDialogShow = DIALOG_EMPTY;
	private int mGlobalShow = GLOBAL_DIALOG_EMPTY;

	private Table mHighScoreTable;
	private Table mInfoTable;
	private Table mResultTable;
	private Table mPauseTable;
	private Table mInputTable;
	private Table mAlertTable;
	private Table mExitTable;
	
	private final MainActivity mActivity;
	public final OAnQuanController mController;
	private OAnQuanScreen mCurrentScreen;
	private final TweenManager mTweenManager;
	private Stage mStage;

	private SettingManager mSetting;
	public AutoRunText mAutoRunText;

	// ===========================================================
	// Constructor
	// ===========================================================
	public UICenter(final OAnQuanController controller, final Stage stage, final TweenManager tweenManager) {
		mTweenManager = tweenManager;
		mStage = stage;
		mController = controller;
		mActivity = mController.getActivity();
		
		init();
	}
	private void init() {
		mButtonClickSound = mController.get(SoundAsset.ASSET_SOUND_FOLDER + SoundAsset.ASSET_SOUND_CLICK, Sound.class);
		
		mLogoImage = StageHelper.createImage(mController.getLogoRegion(), 0, 480 - mController.getLogoRegion().getRegionHeight());
		
		mBlackImage = StageHelper.createImage(mController.getBlackRegion(), 0, 0, 800, 480);
		mBlackImage.setColor(0, 0, 0, 0.6f);
		
		mSetting = new SettingManager();
		mSetting.init(mStage, mTweenManager);
		mSetting.setStyle(SettingManager.TO_DOWN);
		mSetting.setSize(64, 64);
		mSetting.setPad(5);
		mSetting.setXY(0, 350);
		
		Image iconSetting=null;
		ImageButton setting = null, music=null, sound=null, info=null, help=null;

		final Skin skin = mController.get(GameCenter.ASSET_FOLDER_FONT + GameCenter.ASSET_SKIN, Skin.class);
		final TextureAtlas atlas = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_SETTING, TextureAtlas.class);
		
		if (setting == null) {
			setting = StageHelper.createImageButton(skin, atlas.findRegion("0"),
					null, null,  (731), (480-215));
			iconSetting = StageHelper.createImage(atlas.findRegion("1"), 6, 5);
			iconSetting.setOrigin(32, 32);				
			mSetting.addActor(setting,null);
			mSetting.setRotateImage(iconSetting);
		}
		
		if (music == null) {
			music = StageHelper.createImageButton(skin, atlas.findRegion("2"), null, null, 0, 0);
			mSetting.addActor(music, new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					if (GameCenter.isSound) mButtonClickSound.play();
					GameCenter.isMusic = !GameCenter.isMusic;
					((OAnQuanScreen)mCurrentScreen).changeMusicSetting(GameCenter.isMusic);
//					Log.e("m", "music = " + GameCenter.isMusic );
				}
			});
		}
		
		if (sound == null) {
			sound = StageHelper.createImageButton(skin, atlas.findRegion("3"),
					null, null,  (731), (480-282));
			mSetting.addActor(sound,  new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					GameCenter.isSound = !GameCenter.isSound;
					if (GameCenter.isSound) mButtonClickSound.play();
//					Log.e("s", "sound = " + GameCenter.isSound );
				}
			});
		}
		
		if (info == null) {
			info = StageHelper.createImageButton(skin, atlas.findRegion("5"),
					null, null, (731), (480-84));	
			mSetting.addActor(info, new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					if (GameCenter.isSound) mButtonClickSound.play();
					if (mDialogShow != DIALOG_EMPTY) return;
					mStage.addActor(mBlackImage);
					mCurrentScreen.onGameScreenPause();
					showInfo();
				}
			});
		}
		
		if (help == null) {
			help = StageHelper.createImageButton(skin, atlas.findRegion("4"),
					null, null,  (731), (480-150));
			mSetting.addActor(help, new ChangeListener() {
				
				@Override
				public void changed(ChangeEvent arg0, Actor arg1) {
					if (GameCenter.isSound) mButtonClickSound.play();
					if (mDialogShow != DIALOG_EMPTY) return;
					mStage.addActor(mBlackImage);
					mCurrentScreen.onGameScreenPause();
					showHelp();
				}
			});
		}
				
		mSetting.build();
		mStage.addActor(mSetting);
		
		final TextureAtlas popupUi = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_POPUP, TextureAtlas.class);
		final TextureRegion popupHelp = popupUi.findRegion(ImageAssets.REGION_POPUP_HELP);
		mHelpPopup = StageHelper.createImage(popupHelp, 190, 15);
		
		mInfoTable = StageHelper.createTable(skin, 160, 90, 503, 280);
		
		final BitmapFont hiFont = mController.get(GameCenter.ASSET_FOLDER_FONT + GameCenter.ASSET_FONT_HIGH_SCORE, BitmapFont.class);
		final TextureAtlas gameUi = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_GAME_UI, TextureAtlas.class);
		final Image infoBoard = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_DIAGLOG_IMAGE), 0, 0, 503, 280);
		final Label labelInfo = StageHelper.createLabel(hiFont, "       Ô ăn quan\nNhà phát triển Zegome\nPhiên bản 1\nWeb: http://zegome.com", null, 70, 100);
		final ImageButton okInfoButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_DONG_Y), gameUi.findRegion(ImageAssets.REGION_DONG_Y_PRESS), null, 155, 25, 160, 60);
		okInfoButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				mCurrentScreen.onGameScreenResume();
				hideInfo();
			}
		});
		
		mInfoTable.addActor(infoBoard);
		mInfoTable.addActor(labelInfo);
		mInfoTable.addActor(okInfoButton);
		
		mExitTable = StageHelper.createTable(skin, 150, 110, 510, 233);
		
		final Image exitDialog = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_DIAGLOG_IMAGE), 0, 0);
		final Label exitLabel = StageHelper.createLabel(hiFont, "Bạn có chắc chắn muốn thoát\n      khỏi game không?", null, 65, 117);
		final ImageButton okExitButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_DONG_Y), gameUi.findRegion(ImageAssets.REGION_DONG_Y_PRESS), null, 60, 25, 180, 65);
		final ImageButton cancelExitButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_HUY), gameUi.findRegion(ImageAssets.REGION_HUY_PRESS), null, 265, 25, 180, 65);
		
		okExitButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				Gdx.app.exit();
			}
		});
		cancelExitButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				hideExitDialog();
			}
		});
		
		mExitTable.addActor(exitDialog);
		mExitTable.addActor(exitLabel);
		mExitTable.addActor(okExitButton);
		mExitTable.addActor(cancelExitButton);
		
		final Texture texture = mController.get(GameCenter.ASSET_FOLDER_FONT + GameCenter.ASSET_FONT_BITMAP, Texture.class);
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		final TextureRegion tran = new TextureRegion(mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.TRANPARENCY, Texture.class));
		final BitmapFont f = mController.get(GameCenter.ASSET_FOLDER_FONT + GameCenter.ASSET_FONT, BitmapFont.class);
		f.setScale(0.8f);
		
		mAutoRunText = new AutoRunText(mController);
		mAutoRunText.init(mStage, skin, f, tran);
		mAutoRunText.setXY(100, 0);
		mAutoRunText.setWH(600, 50);  
		
		final TextureAtlas buttonAtlas = mController.get(ImageAssets.ASSET_FOLDER_ATLAS + ImageAssets.ATLAS_BUTTON, TextureAtlas.class);
		
		final TextureRegion fbRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_FB_ICON);
		mFbLogin = StageHelper.createImageButton(skin, fbRegion, null, null, 200, 0);
		mFbLogin.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
//				onFbLoginButtonClick();
			}
		});

		mFbRequest = StageHelper.createImageButton(skin, fbRegion, null, null, 350, 0);
		mFbRequest.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
//				onFbRequestButtonClick();
			}
		});

		mFbOpenAction = StageHelper.createImageButton(skin, fbRegion, null, null, 500, 0);
		mFbOpenAction.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
//				onFbOpenAction();
			}
		});

		mFbNewFeed = StageHelper.createImageButton(skin, fbRegion, null, null, 700, 0);
		mFbNewFeed.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
//				onNewFeedRequest();
			}
		});
		
		final TextureRegion backRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_BACK);
		final TextureRegion backRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_BACK_PRESS);
		mBackButton = StageHelper.createImageButton(skin, backRegion, backRegionPress, null, 0, 0);
		mBackButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onBackButtonClick();
			}
		});

		final TextureRegion playRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_PLAY);
		final TextureRegion playRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_PLAY_PRESS);
		mPlayButton = StageHelper.createImageButton(skin, playRegion, playRegionPress, null, 160, 160);
		mPlayButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onPlayButtonClick();
			}
		});
		
		final TextureRegion highscoreRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_HIGH_SCORE);
		final TextureRegion highscoreRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_HIGH_SCORE_PRESS);
		mHighScoreButton = StageHelper.createImageButton(skin, highscoreRegion, highscoreRegionPress, null, 440, 160);
		mHighScoreButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				mStage.addActor(mBlackImage);
				onHighScoreButtonClick();
			}
		});

		final TextureRegion onePlayerRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_ONE_PLAYER);
		final TextureRegion onePlayerRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_ONE_PLAYER_PRESS);
		mOnePlayerButton = StageHelper.createImageButton(skin, onePlayerRegion, onePlayerRegionPress, null, 160, 160);
		mOnePlayerButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onOnePlayerButtonClick();
			}
		});
		
		final TextureRegion twoPlayerRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_TWO_PLAYER);
		final TextureRegion twoPlayerRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_TWO_PLAYER_PRESS);
		mTwoPlayerButton = StageHelper.createImageButton(skin, twoPlayerRegion, twoPlayerRegionPress, null, 440, 160);
		mTwoPlayerButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onTwoPlayerButtonClick();
			}
		});

		final TextureRegion easyModeRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_EASY);
		final TextureRegion easyModeRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_EASY_PRESS);
		mEasyModeButton = StageHelper.createImageButton(skin, easyModeRegion, easyModeRegionPress, null, 160, 160);
		mEasyModeButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onEasyButtonClick();
			}
		});

		final TextureRegion normalModeRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_NORMAL);
		final TextureRegion normalModeRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_NORMAL_PRESS);
		mNormalModeButton = StageHelper.createImageButton(skin, normalModeRegion, normalModeRegionPress, null, 440, 160);
		mNormalModeButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onNormalButtonClick();
			}
		});

		final TextureRegion hardModeRegion = buttonAtlas.findRegion(ImageAssets.BUTTON_HARD);
		final TextureRegion hardModeRegionPress = buttonAtlas.findRegion(ImageAssets.BUTTON_HARD_PRESS);
		mHardModeButton = StageHelper.createImageButton(skin, hardModeRegion, hardModeRegionPress, null, 295, 50);
		mHardModeButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onHardButtonClick();
			}
		});
					
		//+++++++ init playing screen ++++++++//		
		//input name
		
		mInputTable = StageHelper.createTable(skin, 150, 205, 510, 233);
		
		final Image boardInput = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_DIAGLOG_IMAGE), 0, 0);
		final TextField textField = StageHelper.createTextField(skin, 90, 105, 300, 50);
		final Label inputMessage = StageHelper.createLabel(hiFont, "Nhập tên", null, 70, 175);
		final ImageButton okInputButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_DONG_Y), gameUi.findRegion(ImageAssets.REGION_DONG_Y_PRESS), null, 60, 20, 180, 65);
		final ImageButton cancelInputButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_HUY), gameUi.findRegion(ImageAssets.REGION_HUY_PRESS), null, 265, 20, 180, 65);
		
		okInputButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				final String text = new String(textField.getText());
				final ZegomeActivity activity = mActivity;
				if (text.trim().equals("")) {
					activity.onShowToast("Hãy nhập tên", ZegomeActivity.LONG_SHOW);
					return;
				}
				HighScoreHelper.savaHighScore(activity, textField.getText(), ((PlayingScreen)mCurrentScreen).getScore(), ((PlayingScreen)mCurrentScreen).getHighScoreIndex());
				hideInputName();
				resetHighScoreBoard();
				mStage.addActor(mBlackImage);
				showHighScoreBoard();
			}
		});
		cancelInputButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				hideInputName();
				mCurrentScreen.backScreen();
			}
		});
		
		mInputTable.addActor(boardInput);
		mInputTable.addActor(textField);
		mInputTable.addActor(inputMessage);
		mInputTable.addActor(okInputButton);
		mInputTable.addActor(cancelInputButton);
		mController.getInputText().addTextField(textField, true);
		
		//dialog message
		
		mAlertTable = StageHelper.createTable(skin, 150, 205, 510, 233);
		
		final Image boardDialog = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_DIAGLOG_IMAGE), 0, 0);
		mMessageAlert = StageHelper.createLabel(hiFont, "", null, 65, 147);
		final ImageButton okAlertButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_DONG_Y), gameUi.findRegion(ImageAssets.REGION_DONG_Y_PRESS), null, 60, 25, 180, 60);
		final ImageButton cancelAlertButton = StageHelper.createImageButton(skin, gameUi.findRegion(ImageAssets.REGION_HUY), gameUi.findRegion(ImageAssets.REGION_HUY_PRESS), null, 265, 25, 180, 60);
		
		okAlertButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				hideResult();
				hideDialogMessage();
				mStage.addActor(mBlackImage);
				showInputName();
			}
		});
		cancelAlertButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				hideDialogMessage();
				mCurrentScreen.backScreen();
			}
		});
		
		mAlertTable.addActor(boardDialog);
		mAlertTable.addActor(mMessageAlert);
		mAlertTable.addActor(okAlertButton);
		mAlertTable.addActor(cancelAlertButton);
		
		//highscore
		mHighScoreTable = StageHelper.createTable(skin, 140, 0, 504, 480);	
		
		final Table content = StageHelper.createTable(skin, 0, 0, 504, 480);

		final Image boardHighScore = StageHelper.createImage(popupUi.findRegion(ImageAssets.REGION_POPUP_BOARD), 0, 0, 504, 480);
		boardHighScore.setSize(504, 480);
		boardHighScore.setPosition(0, (0));
		mHighScoreTable.addActor(content);

		Table table = StageHelper.createTableWithTransBg(skin, 0, 0, 504, 0);
		
		ScrollPane scroll = new ScrollPane(table);
		scroll.setBounds(0, 30, 504, 340);
		scroll.setScrollingDisabled(true, false);
		table.defaults().pad(2);
		table.row().height(70);

		final Image gomeTop = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_TOP_GOME), 30, 365);
		final Image topText = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_TOP_TEXT), 135, 385);
		
		final Image[][] bgNameMarks = new Image[HighScoreHelper.TOP][2];
		mNameMarks = new Label[HighScoreHelper.TOP][2];

		for (int i = 0; i < HighScoreHelper.TOP; i ++) {
			final int dis = 72 * i;
			bgNameMarks[i][0] = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_HI_BT), 0, 0, 270, 60);
			bgNameMarks[i][1] = StageHelper.createImage(gameUi.findRegion(ImageAssets.REGION_HI_BT_PRESS), 272, 0, 140, 60);
			
			mNameMarks[i][0] = StageHelper.createLabel(hiFont, "Tran Thi Hang", null, 31, 6);
			mNameMarks[i][1] = StageHelper.createLabel(hiFont, "10000", null, 291, 6);
			
			Table item;
			int y = 316 - dis;
			item = StageHelper.createTableWithTransBg(skin, 0, y, 400, 70);
			item.addActor(bgNameMarks[i][0]);
			item.addActor(bgNameMarks[i][1]);
			item.addActor(mNameMarks[i][0]);
			item.addActor(mNameMarks[i][1]);
			
			table.add(item);
			table.row().height(70);
		}
		
		content.addActor(gomeTop);
		content.addActor(topText);
		content.addActor(scroll);

		mHighScoreTable.addActor(boardHighScore);
		mHighScoreTable.addActor(content);
		
		final TextureRegion menuRegion = gameUi.findRegion(ImageAssets.BUTTON_MENU);
		final TextureRegion menuRegionPress = gameUi.findRegion(ImageAssets.BUTTON_MENU_PRESS);

		mPauseTable = StageHelper.createTable(skin, 190, 40, 290, 120);
		final TextureRegion boardRegion = popupUi.findRegion(ImageAssets.REGION_POPUP_BOARD);
		final Image bgMenu = StageHelper.createImage(boardRegion, 0, 0);
		final ImageButton pauseMenuButton = StageHelper.createImageButton(skin, menuRegion, menuRegionPress, null, 115, 255);
		pauseMenuButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onPauseMenuButtonClick();
			}
		});
		
		final TextureRegion continueRegion = gameUi.findRegion(ImageAssets.BUTTON_CONTINUE);
		final TextureRegion continueRegionPress = gameUi.findRegion(ImageAssets.BUTTON_CONTINUE_PRESS);
		final ImageButton continueButton = StageHelper.createImageButton(skin, continueRegion, continueRegionPress, null, 115, 160);
		continueButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onContinueButtonClick();
			}
		});

		final TextureRegion exitRegion = gameUi.findRegion(ImageAssets.BUTTON_EXIT);
		final TextureRegion exitRegionPress = gameUi.findRegion(ImageAssets.BUTTON_EXIT_PRESS);
		final ImageButton exitButton = StageHelper.createImageButton(skin, exitRegion, exitRegionPress, null, 115, 80);
		exitButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onExitButtonClick();
			}
		});

		mPauseTable.addActor(bgMenu);
		mPauseTable.addActor(pauseMenuButton);
		mPauseTable.addActor(continueButton);
		mPauseTable.addActor(exitButton);
		
		mResultTable = StageHelper.createTable(skin, 190, 40, 290, 120);

		final TextureRegion resultRegion = gameUi.findRegion(ImageAssets.REGION_RESULT_TEXT);
		final Image resultText = StageHelper.createImage(resultRegion, 110, 300);
		final Image bgResult = StageHelper.createImage(boardRegion, 0, 0);
		final TextureRegion winBoardRegion = gameUi.findRegion(ImageAssets.REGION_WIN_BOARD);
		mWinBoardImage = StageHelper.createImage(winBoardRegion, 65, 180);
		final TextureRegion drawBoardRegion = gameUi.findRegion(ImageAssets.REGION_DRAW_BOARD);
		mDrawBoardImage = StageHelper.createImage(drawBoardRegion, 65, 180);

		final ImageButton resultMenuButton = StageHelper.createImageButton(skin, menuRegion, menuRegionPress, null, 115, 100);
		resultMenuButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onMenuButtonClick();
			}
		});

		final TextureRegion saveRegion = gameUi.findRegion(ImageAssets.BUTTON_SAVE_SCORE);
		final TextureRegion saveRegionPress = gameUi.findRegion(ImageAssets.BUTTON_SAVE_SCORE_PRESS);
		final ImageButton saveScoreButton = StageHelper.createImageButton(skin, saveRegion, saveRegionPress, null, 115, 30);
		saveScoreButton.addListener(new ChangeListener() {
			
			@Override
			public void changed(ChangeEvent arg0, Actor arg1) {
				if (GameCenter.isSound) mButtonClickSound.play();
				onSaveScoreClick();
			}
		});
		
		mBlackImage = StageHelper.createImage(mController.getBlackRegion(), 0, 0, 800, 480);
		mBlackImage.setColor(0, 0, 0, 0.7f);
		
		final int h = 60;
		final int w1 = 200;
		final int w2 = 90;

		mResultTable.addActor(bgResult);
		final Table resultContent = StageHelper.createTable(skin, 70, 185, 290, 120);
		mResultTable.addActor(resultContent);
		mResultTable.addActor(mWinBoardImage);
		mResultTable.addActor(mDrawBoardImage);
		mResultTable.addActor(resultMenuButton);
		mResultTable.addActor(saveScoreButton);
		mResultTable.addActor(resultText);
		
		mWinPlayerLables = new Label[2];
		mLossPlayerLables = new Label[2];
		
		mWinPlayerLables[0] = new Label("You", skin);
		mWinPlayerLables[0].setBounds(0, 0, w1, h);
		mWinPlayerLables[0].setAlignment(Align.center|Align.bottom);
		mWinPlayerLables[0].setWrap(true);

		mWinPlayerLables[1] = new Label("", skin);
		mWinPlayerLables[1].setBounds(0, 0, w2, h);
		mWinPlayerLables[1].setAlignment(Align.left|Align.bottom);
		mWinPlayerLables[1].setWrap(true);
		
		
		mLossPlayerLables[0] = new Label("Computer", skin);
		mLossPlayerLables[0].setBounds(0, 0, w1, h);
		mLossPlayerLables[0].setAlignment(Align.center|Align.bottom);
		mLossPlayerLables[0].setWrap(true);

		mLossPlayerLables[1] = new Label("", skin);
		mLossPlayerLables[1].setBounds(0, 0, w2, h);
		mLossPlayerLables[1].setAlignment(Align.left|Align.bottom);
		mLossPlayerLables[1].setWrap(true);
		
		resultContent.defaults().pad(2);
		
		resultContent.row().height(h);
		resultContent.add(mWinPlayerLables[0]).width(w1);
		resultContent.add(mWinPlayerLables[1]).width(w2);
		resultContent.row().height(h);
		resultContent.add(mLossPlayerLables[0]).width(w1);
		resultContent.add(mLossPlayerLables[1]).width(w2);
		
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void onComplete(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFailure(String arg0) {
		// TODO Auto-generated method stub
		
	}

	// ===========================================================
	// Methods
	// ===========================================================
	
	//**************************** LOGO & SETTING *********************************//
	public void start() {
//		mUIStack.push(UI_EMPTY);
		mState = UI_MENU_1;
		mLastState = UI_EMPTY;
		mDialogShow = DIALOG_EMPTY;
		mGlobalShow = GLOBAL_DIALOG_EMPTY;
		showAutoBack();
		addLogoSetting();
		showMenu1();
	}
	
	public void show() {
		if (mLastState == UI_MENU_2) {
			showMenu2();
		} else if (mLastState ==UI_MENU_3) {
			showMenu3();
		}
		showAutoBack();
	}
	
	public void resume() {
		mLogoImage = StageHelper.createImage(mController.getLogoRegion(), 0, 480 - mController.getLogoRegion().getRegionHeight());
		mStage.addActor(mLogoImage);
	}
	
	public void pause() {
		mLogoImage.remove();
		mLogoImage = null;
	}
	
	public void dispose() {
		
	}
	
	public boolean back() {
		if (mGlobalShow != GLOBAL_DIALOG_EMPTY) {
			if (mGlobalShow == GLOBAL_HELP) {
				hideHelp();
			}
				
			return true;
		}
		
		if (mDialogShow != DIALOG_EMPTY) {
			switch (mDialogShow) {
			case SHOW_ALERT_EXIT:
				
				return true;
			case SHOW_HIGH_SCORE:
				hideHighScoreBoard();
				if (mState == UI_PLAYING) {
					mCurrentScreen.backScreen();
				}
				return true;
			case SHOW_INDEX_SCORE:
				hideDialogMessage();
				return true;
			case SHOW_SAVE_SCORE:
				hideInputName();
				return true;
			}
		}
		
		switch (mState) {
		case UI_MENU_1:
			showExitDialog();
			return true;

		case UI_MENU_2:
			unShowMenu2();
			showMenu1();
			return true;

		case UI_MENU_3:
			unShowMenu3();
			showMenu2();
			return true;

		case UI_PLAYING:
			
			return false;
		}
		
		return false;
	}
	
	public void addLogoSetting() {
		mStage.addActor(mLogoImage);
		mStage.addActor(mSetting);
	}
	
	public void showAutoBack() {
		mStage.addActor(mBackButton);
		mAutoRunText.show(mStage);
	}
	
	public void hideAutoBack() {
		mBackButton.remove();
		mAutoRunText.hide();
	}
	
	private void showInfo() {
		mGlobalShow = GLOBAL_INFO;
		mStage.addActor(mInfoTable);
	}
	
	private void hideInfo() {
		mGlobalShow = DIALOG_EMPTY;
		if (mState != UI_PLAYING) mBlackImage.remove();
		mInfoTable.remove();
	}
	
	private void showHelp() {
		mGlobalShow = GLOBAL_HELP;
		mStage.addActor(mHelpPopup);
	}
	
	private void hideHelp() {
		mGlobalShow = GLOBAL_DIALOG_EMPTY;
		if (mState != UI_PLAYING) mBlackImage.remove();
		mHelpPopup.remove();
	}
	
	private void showExitDialog() {
		if (mDialogShow == SHOW_ALERT_EXIT) return;
		mDialogShow = SHOW_ALERT_EXIT;
		mStage.addActor(mBlackImage);
		mStage.addActor(mExitTable);
	}
	
	private void hideExitDialog() {
		mDialogShow = DIALOG_EMPTY;
		
		mBlackImage.remove();
		mExitTable.remove();
	}
	
	//***************************** MENU SCREEN ******************************//
	
	void showMenu1() {
		mState = UI_MENU_1;
		mPlayButton.setPosition(-mPlayButton.getWidth(), 160);
		mHighScoreButton.setPosition(GameCenter.CAMERA_WIDTH, 160);

		mStage.addActor(mPlayButton);
		mStage.addActor(mHighScoreButton);
		
		Tween.to(mPlayButton, WidgetAccessor.POS_XY, 0.5f).target(160f, 160f).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mHighScoreButton, WidgetAccessor.POS_XY, 0.5f).target(440f, 160f).ease(Linear.INOUT).start(mTweenManager);
		
//		showFBButton();
	}
	
	private void unShowMenu1() {
		Tween.to(mPlayButton, WidgetAccessor.POS_XY, 0.3f).target(160f, -mPlayButton.getHeight()).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mHighScoreButton, WidgetAccessor.POS_XY, 0.3f).target(440f, -mHighScoreButton.getHeight()).ease(Linear.INOUT)
		.setCallback(new TweenCallback() {
			
			@Override
			public void onEvent(int arg0, BaseTween<?> arg1) {
				mPlayButton.remove();
				mHighScoreButton.remove();
			}
		})
		.start(mTweenManager);
	}
	
	private void showMenu2() {
		mState = UI_MENU_2;

		mOnePlayerButton.setPosition(-mOnePlayerButton.getWidth(), 160);
		mTwoPlayerButton.setPosition(GameCenter.CAMERA_WIDTH, 160);
		
		mStage.addActor(mOnePlayerButton);
		mStage.addActor(mTwoPlayerButton);

		Tween.to(mOnePlayerButton, WidgetAccessor.POS_XY, 0.5f).target(160f, 160f).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mTwoPlayerButton, WidgetAccessor.POS_XY, 0.5f).target(440, 160f).ease(Linear.INOUT).start(mTweenManager);
	}
	
	private void unShowMenu2() {
		Tween.to(mOnePlayerButton, WidgetAccessor.POS_XY, 0.3f).target(160f, -mOnePlayerButton.getHeight()).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mTwoPlayerButton, WidgetAccessor.POS_XY, 0.3f).target(440, -mTwoPlayerButton.getHeight()).ease(Linear.INOUT)
		.setCallback(new TweenCallback() {
			
			@Override
			public void onEvent(int arg0, BaseTween<?> arg1) {
				mOnePlayerButton.remove();
				mTwoPlayerButton.remove();
			}
		})
		.start(mTweenManager);
	}
	
	public void showMenu3() {
		mState = UI_MENU_3;

		mEasyModeButton.setPosition(-mEasyModeButton.getWidth(), 160);
		mNormalModeButton.setPosition(GameCenter.CAMERA_WIDTH, 160);
		mHardModeButton.setPosition(295f, -mHardModeButton.getHeight());

		mStage.addActor(mEasyModeButton);
		mStage.addActor(mNormalModeButton);
		mStage.addActor(mHardModeButton);

		Tween.to(mEasyModeButton, WidgetAccessor.POS_XY, 0.5f).target(160f, 160f).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mNormalModeButton, WidgetAccessor.POS_XY, 0.5f).target(440f, 160f).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mHardModeButton, WidgetAccessor.POS_XY, 0.3f).target(295f, 50f).ease(Linear.INOUT).start(mTweenManager);
	}

	private void unShowMenu3() {
		Tween.to(mEasyModeButton, WidgetAccessor.POS_XY, 0.3f).target(160f, -mEasyModeButton.getHeight()).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mNormalModeButton, WidgetAccessor.POS_XY, 0.3f).target(440f, -mNormalModeButton.getHeight()).ease(Linear.INOUT).start(mTweenManager);
		Tween.to(mHardModeButton, WidgetAccessor.POS_XY, 0.3f).target(295f, -mHardModeButton.getHeight()).ease(Linear.INOUT)
		.setCallback(new TweenCallback() {
			
			@Override
			public void onEvent(int arg0, BaseTween<?> arg1) {
				mEasyModeButton.remove();
				mNormalModeButton.remove();
				mHardModeButton.remove();
			}
		})
		.start(mTweenManager);
	}
	 
	//--------------- Menu Button Click ----------------//	
	private void onBackButtonClick() {
		back();
	}

	private void onPlayButtonClick() {
		unShowMenu1();
		showMenu2();
	}
	private void onHighScoreButtonClick() {
		resetHighScoreBoard();
		showHighScoreBoard();
	}
	
	private void onOnePlayerButtonClick() {
		GameCenter.isNeedAI = true;
		unShowMenu2();
		showMenu3();
	}
	
	private void onTwoPlayerButtonClick() {
		GameCenter.isNeedAI = false;

		mLastState = mState;
		mState = UI_PLAYING;
		unShowMenu2();
		mCurrentScreen.nextScreen();
	}
	
	private void onEasyButtonClick() {
		GameCenter.DEPTH = 0;
		GameCenter.LEVEL = 2;
		GameCenter.TIME_DEFAULT = 25;
		
		unShowMenu3();
		mLastState = mState;
		mState = UI_PLAYING;
		mCurrentScreen.nextScreen();
	}
	
	private void onNormalButtonClick() {
		GameCenter.DEPTH = 1;
		GameCenter.LEVEL = 3;
		
		GameCenter.TIME_DEFAULT = 20;

		unShowMenu3();
		mLastState = mState;
		mState = UI_PLAYING;
		mCurrentScreen.nextScreen();
	}
	
	private void onHardButtonClick() {
		GameCenter.DEPTH = 4;
		GameCenter.LEVEL = 3;
		GameCenter.TIME_DEFAULT = 15;

		unShowMenu3();
		mLastState = mState;
		mState = UI_PLAYING;
		mCurrentScreen.nextScreen();
	}
	
	//***************************** PLAYING SCREEN *****************************//

	private void onPauseMenuButtonClick() {
//		mScreen.backMenuScreen();
		mDialogShow = DIALOG_EMPTY;
		hidePause();
		mCurrentScreen.backScreen();
	}
	
	private void onMenuButtonClick() {
		if (GameCenter.isNeedAI && ((PlayingScreen)mCurrentScreen).getHighScoreIndex() > -1) {
			hideResult();
			showDialogMessage();
		} else {
//			mScreen.backMenuScreen();
			mDialogShow = DIALOG_EMPTY;
			hideResult();
			mCurrentScreen.backScreen();
		}
	}
	
	private void onContinueButtonClick() {
		hidePause();
		((PlayingScreen)mCurrentScreen).continuePlaying();
	}

	private void onExitButtonClick() {
		Gdx.app.exit();
	}
	
	private void onSaveScoreClick() {
		if (GameCenter.isNeedAI) {
			if (((PlayingScreen)mCurrentScreen).getHighScoreIndex() > -1) {
				hideResult();
				mStage.addActor(mBlackImage);
				showInputName();
			} else {
				mActivity.onShowToast("Không thể lưu, bạn còn quá kém để xếp tốp!", ZegomeActivity.SHORT_SHOW);
			}
		} else {
			mActivity.onShowToast("Không thể lưu điểm khi chơi 2 người", ZegomeActivity.SHORT_SHOW);
		}
	}
	
	public void showInputName() {
		mDialogShow = SHOW_SAVE_SCORE;
		
		mStage.addActor(mInputTable);
	}
	
	public void hideInputName() {
		mDialogShow = DIALOG_EMPTY;
		
		mBlackImage.remove();
		mInputTable.remove();
	}
	
	public void showDialogMessage() {
		mDialogShow = SHOW_INDEX_SCORE;
		mStage.addActor(mBlackImage);
		final String mess = "Bạn ghi được " + ((PlayingScreen)mCurrentScreen).getScore() +" điểm\n" 
							+ MESSAGE_REQUEST_HIGH_SCORE[((PlayingScreen)mCurrentScreen).getHighScoreIndex()]
							+ " tốp đại cao thủ.\nBạn có muốn ghi danh không?";
		mMessageAlert.setText(mess);
		
		mStage.addActor(mAlertTable);
	}
	
	public void hideDialogMessage() {
		mDialogShow = DIALOG_EMPTY;
		mBlackImage.remove();
		mAlertTable.remove();
	}
	
	private void resetHighScoreBoard() {
		final String[] names = HighScoreHelper.getNameArray(mActivity);
		final int[] scores = HighScoreHelper.getScoreArray(mActivity);
		
		for (int i = 0; i < HighScoreHelper.TOP; i++) {
			mNameMarks[i][0].setText(names[i]);
			mNameMarks[i][1].setText(String.valueOf(scores[i]));
		}
	}
	
	public void showHighScoreBoard() {
		mDialogShow = SHOW_HIGH_SCORE;
		
		mStage.addActor(mHighScoreTable);
	}
	
	public void hideHighScoreBoard() {
		mDialogShow = DIALOG_EMPTY;
		
		mBlackImage.remove();
		mHighScoreTable.remove();
	}
	
	public void showPause(final boolean isBlack) {
		mDialogShow = SHOW_PAUSE;
		if (isBlack) mStage.addActor(mBlackImage);
		mStage.addActor(mPauseTable);
	}
	
	public void hidePause() {
		mDialogShow = DIALOG_EMPTY;
		mBlackImage.remove();
		mPauseTable.remove();
	}
	
	public void hideResult() {
		mDialogShow = DIALOG_EMPTY;
		mBlackImage.remove();
		mResultTable.remove();
	}
	
	public void showResult(final byte winner, final int firstScore, final int secondScore) {
		result(winner, firstScore, secondScore);
	}
	
	private void result(final int winner, final int firstScore, final int secondScore) {
		mDialogShow = SHOW_RESULT;
		mStage.addActor(mBlackImage);
		if (!GameCenter.isNeedAI) {
			if (winner == PlayingScreen.DRAW) {				
				mDrawBoardImage.setVisible(true);
				mWinBoardImage.setVisible(false);
				mWinPlayerLables[0].setText("Player 1");
				mWinPlayerLables[1].setText(String.valueOf(firstScore));
				mLossPlayerLables[0].setText("Player 2");
				mLossPlayerLables[1].setText(String.valueOf(secondScore));
			} else {
				mDrawBoardImage.setVisible(false);
				mWinBoardImage.setVisible(true);
				mWinPlayerLables[0].setText("Player 1");
				mWinPlayerLables[1].setText(String.valueOf(firstScore));
				mLossPlayerLables[0].setText("Player 2");
				mLossPlayerLables[1].setText(String.valueOf(secondScore));
			} 
		} else {
			if (winner == PlayingScreen.DRAW) {
				mDrawBoardImage.setVisible(true);
				mWinBoardImage.setVisible(false);
				mWinPlayerLables[0].setText("Player 1");
				mWinPlayerLables[1].setText(String.valueOf(firstScore));
				mLossPlayerLables[0].setText("Player 2");
				mLossPlayerLables[1].setText(String.valueOf(secondScore));
			}
			else if (winner == PlayingScreen.FIRST_PLAYER) {
				mDrawBoardImage.setVisible(false);
				mWinBoardImage.setVisible(true);
				mWinPlayerLables[0].setText("You");
				mWinPlayerLables[1].setText(String.valueOf(firstScore));
				mLossPlayerLables[0].setText("Computer");
				mLossPlayerLables[1].setText(String.valueOf(secondScore));
			} else {
				mDrawBoardImage.setVisible(false);
				mWinBoardImage.setVisible(true);
				mWinPlayerLables[0].setText("Computer");
				mWinPlayerLables[1].setText(String.valueOf(secondScore));
				mLossPlayerLables[0].setText("You");
				mLossPlayerLables[1].setText(String.valueOf(firstScore));
			}
		}
		mStage.addActor(mResultTable);
	}

	public void setScreen(OAnQuanScreen oAnQuanScreen) {
		mCurrentScreen = oAnQuanScreen;
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
